/**
 * @file Body_Heath_Sensor.cpp
 * @brief human body health monitoring sensor
 * @author achdu0000
 * @email achdu0000@163.com
 * @version 1.0.0
 * @date 2024/07/01
 */

#include "BodyHealthSensor.h"

bool isAvailable(BodyHeathSensor bhs)
{
    return (bhs.head[0] == 0xff && bhs.head[1] == 0x01);
}

void myBodyHealthSensorSetup()
{
    _BODY_HEALTH_SERIAL.begin(9600);
    while (!_BODY_HEALTH_SERIAL)
        ;
}

void myBodyHeathSensorLoop()
{
    if (sendStartSeqFlag)
    {
        SEND_START_SEQ();
        Serial.println("_BODY_HEALTH_SERIAL.write(\"0xA5A1\")");
        sendStartSeqFlag = false;
    }

    if (_BODY_HEALTH_SERIAL.available())
    {
        Serial.println("_BODY_HEALTH_SERIAL.available()");
        BodyHeathSensor bhs;
        int data_len = _BODY_HEALTH_SERIAL.readBytes((uint8_t *)&bhs, sizeof(BodyHeathSensor));
        Serial.print("data_len: ");
        Serial.println(data_len);

        if (data_len != 16 || !isAvailable(bhs))
        {
            Serial.println("!_BODY_HEALTH_SERIAL.readBytes(my_data, 16)");
            return;
        }
        Serial.print("  first  byte(head1):   ");
        Serial.print("0x");
        Serial.println(bhs.head[0], HEX);
        Serial.print("  second byte(head2):   ");
        Serial.print("0x0");
        Serial.println(bhs.head[1], HEX);
        Serial.print("  heart rate:           ");
        Serial.println(bhs.heartRate);
        Serial.print("  blood oxygen:         ");
        Serial.println(bhs.bloodOxygen);
        Serial.print("  microcirculation:     ");
        Serial.println(bhs.microcirculation);
        Serial.print("  blood pressure(high): ");
        Serial.println(bhs.bloodPressure[0]);
        Serial.print("  blood pressure(low ): ");
        Serial.println(bhs.bloodPressure[1]);
        Serial.print("  R-to-R interval:      ");
        Serial.println(bhs.R2RInterval);
        Serial.print("  HRV-SDNN:             ");
        Serial.println(bhs.HRV_SDNN);
        Serial.print("  HRV-RMSSD:            ");
        Serial.println(bhs.HRV_RMSSD);
        Serial.print("  fatigue value:        ");
        Serial.println(bhs.fatigue);
        Serial.print("  ambient temperature:  ");
        Serial.print(bhs.ambientTemp[0]);
        Serial.print('.');
        Serial.println(bhs.ambientTemp[1]);
        Serial.print("  body    temperature:  ");
        Serial.print(bhs.bodyTemp[0]);
        Serial.print('.');
        Serial.println(bhs.bodyTemp[1]);
        Serial.print("  last byte(tail):      ");
        Serial.print("0x");
        Serial.println(bhs.tail, HEX);
        //    END();
        //    while(1);

        //    START();
    }
    else
    {
        //    Serial.println("!_BODY_HEALTH_SERIAL.available()");
    }
    delay(50);
}
